package com.sicilin.system.auth.service;

import com.sicilin.common.enums.ResultStateEnum;
import com.sicilin.common.exception.BusinessException;
import com.sicilin.common.exception.ServiceException;
import com.sicilin.dao.AuthDao;
import com.sicilin.po.TSysAuth;
import com.sicilin.system.auth.dto.TreeAuth;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,readOnly = true)
public class AuthService {

    @Autowired(required = false)
    private AuthDao authDao;

    @Transactional(rollbackFor = {Exception.class},propagation = Propagation.REQUIRED)
    public Integer saveAuth(String authCode, String authName, int parentId, int authType, int level)throws ServiceException,BusinessException{
        if(StringUtils.isBlank(authCode)){throw new ServiceException("Invalid authCode!");}
        if(StringUtils.isBlank(authName)){throw new ServiceException("Invalid authName!");}
        TSysAuth auth = new TSysAuth();
        auth.setAuthCode(authCode);
        auth.setAuthName(authName);
        auth.setParentId(parentId);
        auth.setAuthTotalCode(authCode);
        auth.setAuthType(authType);
        auth.setLevel(authType == 0?99:level); //如果为权限文件夹，则权限默认为99
        if(parentId != 0){
            TSysAuth parentAuth = authDao.getAuthById(parentId);
            if(parentAuth == null){
                throw new BusinessException("该权限所选择的父级不存在！");
            }
            auth.setAuthTotalCode(parentAuth.getAuthTotalCode()+":"+authCode);
            if(parentAuth.getHasChildren() == 0) {
                this.updateHasChildren(parentId);
            }
        }
        if(this.isExistAuthTotalCode(auth.getAuthTotalCode())){
            throw new BusinessException("已经有相同的绝对路径的权限了！");
        }
        authDao.saveAuth(auth);
        return auth.getAuthId();
    }

    @Transactional(rollbackFor = {Exception.class})
    public Integer updateAuth(int authId,String authName,int authType, int level)throws ServiceException,BusinessException{
        if(StringUtils.isBlank(authName)){throw new ServiceException("Invalid authName!");}
        TSysAuth auth = authDao.getAuthById(authId);
        if(auth == null){throw new BusinessException("找不到需要修改的权限！");}

        return authDao.updateAuth(authId,authName,authType,authType == 0?99:level);

    }

    @Transactional(rollbackFor = {Exception.class})
    public ResultStateEnum deleteAuth(Integer authId) throws ServiceException{
        if(authId == null){throw new ServiceException("Invalid authId!");}
        if(!this.hasBrother(authId)){ //当没有兄弟权限的时候，修改hasChildren属性
            TSysAuth parent = authDao.getParentAuthBySonId(authId);
            if(parent != null){
                this.updateHasNoChildren(parent.getAuthId());
            }
        }
        //删除角色权限
        authDao.deleteRoleAuth(authId);
        return ResultStateEnum.instances(authDao.deleteAuth(authId));
    }


    public boolean isExistAuthTotalCode(String authTotalCode) throws ServiceException{
        if(StringUtils.isBlank(authTotalCode)){throw new ServiceException("Invalid authTotalCode! It's blank!");}
        int result = authDao.existAuthTotalCode(authTotalCode);
        if(result > 0 ){return true;}
        return false;
    }

    public boolean hasBrother(int authId) throws ServiceException{
        if(authId == 0){throw new ServiceException("Invalid authId, It's 0!");}
        if(authDao.countBrotherAuthById(authId) > 0){
            return true;
        }
        return false;
    }

    public List<TreeAuth> getTreeAuth()throws ServiceException{
        return authDao.getTreeAuth();
    }

    public List<TreeAuth> getTreeAuthWidthCheckedByRoleId(int roleId) throws ServiceException{
        return authDao.getTreeAuthWidthCheckedByRoleId(roleId);
    }


    @Transactional(rollbackFor = {Exception.class},propagation = Propagation.REQUIRED)
    public int updateHasChildren(int authId) throws ServiceException{
        if(authId == 0){throw new ServiceException("Invalid authId! It's 0!");}
        return authDao.updateAuthHasChildren(authId,1);
    }

    @Transactional(rollbackFor = {Exception.class},propagation = Propagation.REQUIRED)
    public int updateHasNoChildren(int authId) throws ServiceException{
        if(authId == 0){throw new ServiceException("Invalid authId! It's 0!");}
        return authDao.updateAuthHasChildren(authId,0);
    }



}
